package com.lanou.myapplication.database;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lanou.myapplication.database.greendao.DaoSession;
import com.lanou.myapplication.database.greendao.PersonEntity;
import com.lanou.myapplication.database.greendao.PersonEntityDao;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/11/5.
 */
public class UserDBHelper {
    private static UserDBHelper helper;
    private static DaoSession daoSession;
    private UserDBHelper(){

    }

    public static UserDBHelper getInstance() {
        if (helper == null) {
            helper = new UserDBHelper();
            daoSession = GrenDaoApplication.getDaoSession();
        }


        return helper;

    }

    public Cursor queryAll() {
        SQLiteDatabase database = daoSession.getPersonEntityDao().getDatabase();
        String tabName = daoSession.getPersonEntityDao().getTablename();
        Cursor cursor = database.query(tabName, null, null, null, null, null, null);

        return cursor;

    }


    public void addData(PersonEntity entity) {
        daoSession.getPersonEntityDao().insert(entity);
    }
    public List<PersonEntity> queryAllData() {
        return daoSession.getPersonEntityDao().loadAll();

    }

    public List<PersonEntity> querByName(String name) {

        QueryBuilder<PersonEntity> db = daoSession.getPersonEntityDao().queryBuilder();
        db.where(PersonEntityDao.Properties.Name.eq(name));

        return db.list();

    }

}
